// pages/mine/exchange/exchange.js


let {
  api_ExchangeYisen
} = require("../../api/getData.js")
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    favourate_cnt: 1245,
    yisen_cnt: 45,
    favourate_cost:0,
    yisen_get: 0,
    exchangeUserInfo:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let params = JSON.parse(options.exchangeUserInfo);
    this.setData({
      exchangeUserInfo:params,
    })
  },
 
  bindYisenChange: function (event){
    var newData = event.detail.value
    newData = parseInt(newData);
    if(isNaN(newData)){
      console.log("請輸入整數");
      newData = 0;
    }
    var favourate_cost_new = newData*20;
    this.setData({
      favourate_cost: favourate_cost_new,
      yisen_get: newData
    })
  },

  bindExchangeYisen:function(){
    var data = {
      userid: app.globalData.userInfo.id,
      favourate_cnt: this.data.favourate_cost
    }
    api_ExchangeYisen(data, (res) => {
      if(res.status==200){
        var tmp = this.data.exchangeUserInfo;
        tmp.favourate_cnt = tmp.favourate_cnt-this.data.favourate_cost;
        tmp.yisen_cnt = tmp.yisen_cnt + this.data.yisen_get
        this.setData({
          exchangeUserInfo:tmp,
        })
        wx.showToast({
          title: '兑换成功',
          duration:2000
        })
      }else{
        wx.showToast({
          title: '兑换失败',
          duration: 2000
        })
      }
    })
  }


})